package com.liveneo.stoat.common.web;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.servlet.mvc.support.RedirectAttributesModelMap;

import com.liveneo.stoat.common.tag.Msg;
import com.liveneo.stoat.common.util.AjaxResult;
import com.liveneo.stoat.common.util.Convert;
import com.liveneo.stoat.common.util.StrUtils;
import com.liveneo.stoat.common.util.WafRequestWrapper;

public class SpringControllerSupport {
	 /**
     * 日志对象
     */
    protected Logger logger = LoggerFactory.getLogger(getClass());
    
	protected HttpServletRequest request;  
    protected HttpServletResponse response;  
    protected HttpSession session;  
      
    @ModelAttribute  
    public void setReqAndRes(HttpServletRequest request, HttpServletResponse response){  
        this.request = request;  
        this.response = response;  
        this.session = request.getSession();  
    }  
    
    protected HttpServletRequest getRequest() {
		return new WafRequestWrapper(this.request);
	}
    
    public boolean isAjax() {
		String header = getRequest().getHeader("X-Requested-With");
		boolean isAjax = "XMLHttpRequest".equalsIgnoreCase(header);
		return isAjax;
	}
	
	public boolean isPost() {
		String method = getRequest().getMethod();
		return StrUtils.equalsIgnoreCase("POST", method);
	}
   
	/**
	 * 根据参数名从HttpRequest中获取String类型的参数值，无值则返回null .
	 * 
	 * @param key
	 *            .
	 * @return String or null .
	 */
	public String getString(String key) {
		return getRequest().getParameter(key);
	}
	public String[] getStrings(String key) {
		return getRequest().getParameterValues(key);
	}
	public Integer getParameterToInt(String name) {
		return Convert.toInt(getRequest().getParameter(name));
	}

	public Integer getParameterToInt(String name, Integer defaultValue) {
		return Convert.toInt(getRequest().getParameter(name), defaultValue);
	}

	public Long getParameterToLong(String name) {
		return Convert.toLong(getRequest().getParameter(name));
	}

	public Long getParameterToLong(String name, Long defaultValue) {
		return Convert.toLong(getRequest().getParameter(name), defaultValue);
	}

	public Float getParameterToFloat(String name) {
		return Convert.toFloat(getRequest().getParameter(name));
	}

	public Float getParameterToFloat(String name, Float defaultValue) {
		return Convert.toFloat(getRequest().getParameter(name), defaultValue);
	}
	
/** ============================     ajax  start  =================================================  */
	
	/**   
	 * 返回ajaxresult
	 * @param data
	 * @return AjaxResult
	*/
	public AjaxResult json(Object data) {
		return new AjaxResult().success(data);
	}
	
	/**   
	 * 返回ajaxresult
	 * @param data
	 * @param message
	 * @return AjaxResult
	*/
	public AjaxResult json(Object data, String message) {
		return json(data).setMessage(message);
	}
	
	/**   
	 * 返回ajaxresult
	 * @param data
	 * @param message
	 * @param code
	 * @return AjaxResult
	*/
	public AjaxResult json(Object data, String message, int code) {
		return json(data, message).setCode(code);
	}
	
	/**   
	 * 返回ajaxresult
	 * @param message
	 * @return AjaxResult
	*/
	public AjaxResult success(String message) {
		return new AjaxResult().addSuccess(message);
	}
	
	/**   
	 * 返回ajaxresult
	 * @param message
	 * @return AjaxResult
	*/
	public AjaxResult error(String message) {
		return new AjaxResult().addError(message);
	}
	
	/**   
	 * 返回ajaxresult
	 * @param message
	 * @return AjaxResult
	*/
	public AjaxResult warn(String message) {
		return new AjaxResult().addWarn(message);
	}
	
	/**   
	 * 返回ajaxresult
	 * @param message
	 * @return AjaxResult
	*/
	public AjaxResult fail(String message) {
		return new AjaxResult().addFail(message);
	}
	/** ============================     ajax  end  =================================================  */
	
	/****      跳转路径      ***/
	public String redirect(String path, String pre){
		return StrUtils.format(path, pre);
	}
	
	/** ===========================         msg start         ==========================================  */
	public void putSuccessMsg(RedirectAttributesModelMap modelMap, String content){
		modelMap.addFlashAttribute("msg", new Msg(Msg.MsgType.success, content));
	}
	public void putInfoMsg(RedirectAttributesModelMap modelMap, String content){
		modelMap.addFlashAttribute("msg", new Msg(Msg.MsgType.info, content));
	}
	public void putErrorMsg(RedirectAttributesModelMap modelMap, String content){
		modelMap.addFlashAttribute("msg", new Msg(Msg.MsgType.error, content));
	}
	public void putDangerMsg(RedirectAttributesModelMap modelMap, String content){
		modelMap.addFlashAttribute("msg", new Msg(Msg.MsgType.danger, content));
	}
	/** ===========================         msg end         ==========================================  */
}
